package cn.gdcp.lkqapp09;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.JsonObject;


import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
    private final String TAG = "MainActivity";
    private Button btnGson1;
    private Button btnGson2;
    private Button btnGson3;
    private Button btnGson4;
    private EditText editCode;

    private String jsonstr = "{\"animals\": {\n" +
            "    \"dog\": [\n" +
            "        {\n" +
            "            \"name\": \"koko\",\n" +
            "            \"breed\": \"labrador\",\n" +
            "            \"count\": \"1\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"name\": \"jojo\",\n" +
            "            \"breed\": \"whippet\",\n" +
            "            \"count\": \"1\"\n" +
            "        }\n" +
            "    ],\n" +
            "    \"cat\": {\"name\": \"kity\"}\n" +
            "}}";

    private String jsoncitystr = "{\"time\":\"2019-03-26 19:09:02\",\"cityInfo\":{\"city\":\"广州市\",\"cityId\":\"101280101\",\"parent\":\"广东\",\"updateTime\":\"18:44\"},\"date\":\"20190326\",\"message\":\"Success !\",\"status\":200,\"data\":{\"shidu\":\"82%\",\"pm25\":48.0,\"pm10\":90.0,\"quality\":\"良\",\"wendu\":\"23\",\"ganmao\":\"极少数敏感人群应减少户外活动\",\"yesterday\":{\"date\":\"25\",\"sunrise\":\"06:28\",\"high\":\"高温 19.0℃\",\"low\":\"低温 14.0℃\",\"sunset\":\"18:40\",\"aqi\":19.0,\"ymd\":\"2019-03-25\",\"week\":\"星期一\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},\"forecast\":[{\"date\":\"26\",\"sunrise\":\"06:27\",\"high\":\"高温 26.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:40\",\"aqi\":70.0,\"ymd\":\"2019-03-26\",\"week\":\"星期二\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"27\",\"sunrise\":\"06:26\",\"high\":\"高温 26.0℃\",\"low\":\"低温 19.0℃\",\"sunset\":\"18:41\",\"aqi\":58.0,\"ymd\":\"2019-03-27\",\"week\":\"星期三\",\"fx\":\"东南风\",\"fl\":\"3-4级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"28\",\"sunrise\":\"06:25\",\"high\":\"高温 27.0℃\",\"low\":\"低温 20.0℃\",\"sunset\":\"18:41\",\"aqi\":54.0,\"ymd\":\"2019-03-28\",\"week\":\"星期四\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"29\",\"sunrise\":\"06:24\",\"high\":\"高温 27.0℃\",\"low\":\"低温 19.0℃\",\"sunset\":\"18:41\",\"aqi\":69.0,\"ymd\":\"2019-03-29\",\"week\":\"星期五\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"中雨\",\"notice\":\"记得随身携带雨伞哦\"},{\"date\":\"30\",\"sunrise\":\"06:23\",\"high\":\"高温 26.0℃\",\"low\":\"低温 17.0℃\",\"sunset\":\"18:42\",\"aqi\":69.0,\"ymd\":\"2019-03-30\",\"week\":\"星期六\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"雷阵雨\",\"notice\":\"带好雨具，别在树下躲雨\"},{\"date\":\"31\",\"sunrise\":\"06:22\",\"high\":\"高温 23.0℃\",\"low\":\"低温 15.0℃\",\"sunset\":\"18:42\",\"aqi\":74.0,\"ymd\":\"2019-03-31\",\"week\":\"星期日\",\"fx\":\"北风\",\"fl\":\"4-5级\",\"type\":\"中雨\",\"notice\":\"记得随身携带雨伞哦\"},{\"date\":\"01\",\"sunrise\":\"06:21\",\"high\":\"高温 21.0℃\",\"low\":\"低温 13.0℃\",\"sunset\":\"18:42\",\"ymd\":\"2019-04-01\",\"week\":\"星期一\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"02\",\"sunrise\":\"06:20\",\"high\":\"高温 22.0℃\",\"low\":\"低温 14.0℃\",\"sunset\":\"18:43\",\"ymd\":\"2019-04-02\",\"week\":\"星期二\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"03\",\"sunrise\":\"06:19\",\"high\":\"高温 26.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:43\",\"ymd\":\"2019-04-03\",\"week\":\"星期三\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"04\",\"sunrise\":\"06:18\",\"high\":\"高温 27.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:43\",\"ymd\":\"2019-04-04\",\"week\":\"星期四\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"05\",\"sunrise\":\"06:18\",\"high\":\"高温 28.0℃\",\"low\":\"低温 20.0℃\",\"sunset\":\"18:44\",\"ymd\":\"2019-04-05\",\"week\":\"星期五\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"阴\",\"notice\":\"不要被阴云遮挡住好心情\"},{\"date\":\"06\",\"sunrise\":\"06:17\",\"high\":\"高温 30.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:44\",\"ymd\":\"2019-04-06\",\"week\":\"星期六\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"07\",\"sunrise\":\"06:16\",\"high\":\"高温 31.0℃\",\"low\":\"低温 24.0℃\",\"sunset\":\"18:45\",\"ymd\":\"2019-04-07\",\"week\":\"星期日\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"08\",\"sunrise\":\"06:15\",\"high\":\"高温 28.0℃\",\"low\":\"低温 23.0℃\",\"sunset\":\"18:45\",\"ymd\":\"2019-04-08\",\"week\":\"星期一\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"09\",\"sunrise\":\"06:14\",\"high\":\"高温 27.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:45\",\"ymd\":\"2019-04-09\",\"week\":\"星期二\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"}]}}";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnGson1 = findViewById(R.id.btn_gson1);
        btnGson2 = findViewById(R.id.btn_gson2);
        btnGson3 = findViewById(R.id.btn_gson3);
        btnGson4 = findViewById(R.id.btn_gson4);
        editCode = findViewById(R.id.edt_code);



        btnGson1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                User user = new User(11, "Lisa", 16);
                String userjsonstr = gson.toJson(user);
                Log.e(TAG, "userjsonstr = " + userjsonstr);

                try {
                    JSONObject jsonObject = new JSONObject(userjsonstr);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                User user2 = gson.fromJson(userjsonstr, User.class);
                Log.e(TAG, "user2 = " + user2.toString());


                String intJsonStr = gson.toJson(123);
                String boolJsonStr = gson.toJson(true);
                String stringJsonStr = gson.toJson("张三丰");
                Log.e(TAG, "intJsonStr = " + intJsonStr);
                Log.e(TAG, "boolJsonStr = " + boolJsonStr);
                Log.e(TAG, "stringJsonStr = " + stringJsonStr);

            }
        });


        btnGson2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                Root root = gson.fromJson(jsonstr, Root.class);
                String catName = root.getAnimals().getCat().getName();
                Log.e(TAG, "catName = " + catName);
            }
        });

        btnGson3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                Weather weather = gson.fromJson(jsoncitystr, Weather.class);
                String type = weather.getData().getForecast().get(0).getType();
                Log.e(TAG, "type = " + type);
            }
        });

//        http://t.weather.sojson.com/api/weather/city/
        btnGson4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = editCode.getText().toString();
                final String url = "http://t.weather.sojson.com/api/weather/city/" + code;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        useHttpGet(url);

                    }
                }).start();
            }
        });
    }

    private void useHttpGet(String url) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            int code = httpResponse.getStatusLine().getStatusCode();
            Log.e(TAG,"code = " + code);
            /*String htmlContent = getHtmlContent(httpResponse);
            Log.e(TAG, htmlContent);*/

            String htmlContent = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
            Log.e(TAG, htmlContent);

            Gson gson = new Gson();
            Weather weather2 = gson.fromJson(htmlContent, Weather.class);
            String type = weather2.getData().getForecast().get(0).getType();
            Log.e(TAG, "type = " + type);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
